<?php

require 'common.php';

if (strpos($_GET['id'],'.xml') !== false)	
 	$filename = $_GET['id'];
else
 	$filename = "xml\\".$_GET['id'].".xml";
		
$show_multiple = false;
if( isset($_GET['multiple']))
	$show_multiple = true ;


$project =  ReadProjectData($filename);

$project_sdate = $project->date;
$project_edate = $project_sdate;//$project_sdate + 1000000;


/*
foreach ($project->subprojects as $subproject) 
			{
				foreach ($subproject->milestones as $milestone) 	
				{
					foreach ($milestone->logs as $log)
					{
					 	echo $log->date." ";
					 	echo $log->days.'<br>';
					}
				}

			}

*/

?>
<!doctype html>
<html lang="en-au">
<head>
<title>jQuery.Gantt</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" >
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.css" />
<link rel="stylesheet" href="css/prettify.css" />
		

<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0" bgcolor="#FFFFFF">

<div class="gantt"></div>

<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.fn.gantt.js"></script>
<script src="js/prettify.js"></script>
<script src="jsalert/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="jsalert/jquery.alerts.js" type="text/javascript"></script>
<link href="jsalert/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />
 

<script>

function MessageLog(engineer,log)
{
	this.engineer=engineer;
	this.log=log;
}



$(function() 
{
	"use strict";
	$(".gantt").gantt({
		source: [
/*********************************************************************/
<?php

function AddProjectDivider($name)
{
	global $project_sdate;
	$sdate = strtotime($project_sdate);
	$sdate = $sdate*1000;
	$edate = $sdate;

	echo '{';
	echo 'name: "'.$name.'",';
	echo 'desc: " ",';
	echo 'entryCssClass : "ganttProject",';	
	echo 'descCssClass : "ganttProject",';	
	echo 'values: [';
	echo		'{';
	echo		'from: "/Date('. $sdate  .')/",';
	echo		'to: "/Date('. $edate .')/",';
	echo		'label: " ",
				customClass: "ganttWhite"
			}';
	echo ']},';

}


function AddSubProject($name)
{
	global $project_sdate;
	$sdate = strtotime($project_sdate);
	$sdate = $sdate*1000;
	$edate = $sdate;

	echo '{';
	echo 'name: "'.$name.'",';
	echo 'desc: " ",';
	echo 'entryCssClass : "ganttSubProject",';	
	echo 'descCssClass : "ganttItalic",';	
	
	echo 'values: [';
	echo		'{';
	echo		'from: "/Date('. $sdate  .')/",';
	echo		'to: "/Date('. $edate .')/",';
	echo		'label: "",
				customClass: "ganttWhite"
			}';
	echo ']},';

}


function AddMilestone($milestone)
{
	global $project_sdate;
	$sdate = strtotime($project_sdate);
	$sdate = $sdate*1000;
	$edate = $sdate;


	echo '{';
	echo 'name: "'.$milestone->name.'",';
	echo 'desc: "'.$milestone->assignee.'",';
	if($milestone->status == "Done")
	{
		echo 'entryCssClass : "ganttStrikeThrough",';	
	}

	
	echo 'values: [';
	echo		'{';
	echo		'from: "/Date('. $sdate  .')/",';
	echo		'to: "/Date('. $edate .')/",';
	echo		'label: "",
				customClass: "ganttWhite"
			}';
	echo ']},';

}

function AddLog($milestone,$log)
{
		$sdate =  strtotime($log->date);
		$formated_date = date("F j, Y", $sdate);

		$days = intval($log->days,0);
		$days = $days - 1;
		$edate = strtotime("+".$days." days",strtotime($log->date));

			
		$sdate = $sdate*1000;
		$edate = $edate*1000;

		echo '{';
		echo    'from: "/Date('.$sdate.')/",';
		echo	'to: "/Date('.$edate.')/",';


		
		echo 'dataObj:"'.$log->engineer." - ".$formated_date."<br>".$log->msg.'",';
		
		if($milestone->status == "Done")
			echo	'label: " ",	customClass: "ganttGreyed"	}';

		else if($milestone->status == "In Progress")
			echo	'label: " ",	customClass: "ganttGreen"	}';
		else
			echo	'label: " ",	customClass: "ganttRed"	}';
}

function AppendLog($milestone,$log)
{
		$sdate =  strtotime($log->date);
		$formated_date = date("F j, Y", $sdate);

		$days = intval($log->days,0);
		$days = $days - 1;
		$edate = strtotime("+".$days." days",strtotime($log->date));
		
		$sdate = $sdate*1000;
		$edate = $edate*1000;

		echo ',{';
		echo    'from: "/Date('.$sdate.')/",';
		echo	'to: "/Date('.$edate.')/",';
		//$formated_date = date("F j, Y", $sdate);
		echo 'dataObj:"'.$log->engineer." - ".$formated_date."<br>".$log->msg.'",';

		if($milestone->status == "Done")
			echo	'label: " ",	customClass: "ganttGreyed"	}';

		else if($milestone->status == "In Progress")
			echo	'label: " ",	customClass: "ganttGreen"	}';
		else
			echo	'label: " ",	customClass: "ganttRed"	}';
}


function AddLogTrailer()
{

	echo ']},';
}
function AddLogHeader($log,$milestone)
{
	global $engineer_tags;


	$name = $engineer_tags[$log->engineer];

	echo '{';
	//echo 'name: "'." ".'",';
	if($milestone != "none")
		echo 'name: "'.$milestone->name.'",';
	echo 'desc: "'.$name.'",';

	if($milestone != "none")
	{
		if($milestone->status == "Done")
		{
			echo 'entryCssClass : "ganttStrikeThrough",';	
		}
	}

	echo 'values: [';
}




foreach ($project->subprojects as $subproject) 
{
	if($subproject->show)
		AddSubProject($subproject->name);
	if(($subproject->type == 'partition') && ($show_multiple))
		AddProjectDivider($subproject->name);

	foreach ($subproject->milestones as $milestone) 	
	{
		$state = "newlog";	
	
		//AddMilestone($milestone);
		foreach ($milestone->logs as $log)
		{
			if($state == "newlog")
			{
				AddLogHeader($log,$milestone);	
				AddLog($milestone,$log);
				$state = $log->engineer;
			}
			else if($state == $log->engineer)
	 			AppendLog($milestone,$log);
	 		else
	 		{
	 			AddLogTrailer();
	 			AddLogHeader($log,"none");	
				AddLog($milestone,$log);
				$state = $log->engineer;
	 		}	
		}
		if($state != "newlog" )
			AddLogTrailer();
	}
}
?>		
		],

		navigate: "scroll",
		scale: "days",
		maxScale: "months",
		minScale: "days",
		itemsPerPage: 200,
		scrollToToday : true,

		onItemClick: function(data) {
			var datastring = String(data);
			var title=datastring.split("<br>",2);
			jAlert(title[1],"Log By "+title[0]);
 
		},
		
		onRender: function() {
			if (window.console && typeof console.log === "function") {
				console.log("chart rendered");
			}
		}
	});

	$(".gantt").popover({
		selector: ".bar",
		title: "I'm a popover",
		content: "And I'm the content of said popover.",
		trigger: "hover"
	});
});
 </script>



